import request from "@/utils/request";

/**
 * 商品分页查询
 * @param {*} params 
 * @returns 
 */
export function productsPageQuery(params) {
    return request({
        url: "/admin/products/pageQuery",
        method: "post",
        data: params
    })
}

/**
 * 查询全部商品
 * @returns 
 */
export function getAllProducts() {
    return request({
        url: "/admin/products/getAll",
        method: 'get'
    })
}

/**
 * 创建商品
 * @param {*} params 
 * @returns 
 */
export function productCreate(params) {
    return request({
        url: "/admin/products/add",
        method: 'post',
        data: params
    })
}

/**
 * 更新商品数据
 * @param {*} param 
 * @param {*} params 
 * @returns 
 */
export function updateProduct(param, params) {
    return request({
        url: `/admin/products/update/${param}`,
        method: 'put',
        data: params
    })
}

/**
 * 商品图像上传
 * @param {*} productId 
 * @param {*} imageFile 
 * @returns 
 */
export function uploadProductImage(productId, imageFile) {
    const params = new FormData();
    params.append("file", imageFile);
    params.append("entityId", String(productId));
    params.append("entityType", "product");
    return request({
        url: '/admin/file/upload',
        method: 'post',
        data: params,
        headers: {
            "Content-Type": "multipart/form-data"
        }
    })
}

/**
 * 更新商品图片资源
 * @param {*} productId 
 * @param {*} productImageUrl 
 * @returns 
 */
export function updateProductImageUrl(productId, productImageUrl) {
    return request({
        url: `/admin/products/updateImage/${productId}`,
        method: 'put',
        data: {
            productImage: productImageUrl
        }
    })
}

/**
 * 删除商品
 * @param {*} param 
 * @returns 
 */
export function deleteProduct(param) {
    return request({
        url: `/admin/products/delete/${param}`,
        method: 'delete',
    })
}

/**
 * 搜索商品
 * @param {*} param 
 * @returns 
 */
export function searchProduct(param) {
    return request({
        url: `/admin/products/searchProduct/`,
        method: 'get',
        params: {
            "keyword": param
        }
    })
}